let that = null;
let mine = require('../../../api/mine.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatarUrl: '',//头像
        nickName: '',//网名
        gender: '',//性别
        stuNum: '',//学号
        stuNumTip: '',//学号出错显示文字
        realName: '',//真实姓名
        realNameTip: '',//真实姓名出错显示文字
        dormNum: '',//宿舍号
        dormNumTip: '',//宿舍号出错显示文字
        phone: '',//电话
        phoneTip: '',//点火出错显示文字
        to: '',//通过个人信息去到哪里
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let to = options.to;// 通过个人信息要去到哪里
        // 判断是否是点击维修后进入该页面的，是的话出提示
        if (to == '/miniprogram/pages/CA/useRepair/userRepair') {
            wx.showToast({
                title: '个人信息需先填写完整',
                icon: 'none',
                duration: 2000,
            })
        }
        that = this;
        let user = wx.getStorageSync('user')
        that.setData({
            avatarUrl: user.avatarUrl,
            nickName: user.nickName,
            stuNum: user.stuNum,
            realName: user.realName,
            gender: user.gender,
            dormNum: user.dormNum,
            phone: user.phone,
            to: to,
        });
    },

    /**
     * 学号判断
     */
    async stuNumBlur(e) {
        let stuNum = String(parseInt(e.detail.value));
        // 是否没输入
        if (stuNum.length < 10) {
            await that.setData({
                stuNumTip: '请输入10位学号',
            })
        } else {
            await that.setData({
                stuNumTip: '',
            })
        }
    },

    /**
     * 真实姓名
     */
    async realNameBlur(e) {
        let realName = e.detail.value;
        const chinese = /[^\u4E00-\u9FA5]/g;
        // 判断的是否全中文
        if (chinese.test(realName) || realName == '') {
            await that.setData({
                realNameTip: '请输入中文名字',
            })
        } else {
            await that.setData({
                realNameTip: '',
            })
        }
    },

    /**
     * 性别
     */
    async genderChange(e) {
        let gender = e.detail.value;
        await that.setData({
            gender: gender,
        })
    },

    /**
     * 宿舍号
     */
    async dormNumBlur(e) {
        let dormNum = e.detail.value;
        const en = /[^A-Za-z0-9]/g;
        const eng = /[^A-Za-z]/g
        // 判断输入宿舍号格式是否正确
        if (en.test(that.data.dormNum) || dormNum.length > 5 || dormNum.length < 4 || eng.test(dormNum.slice(0, 1)) || dormNum == '') {
            await that.setData({
                dormNumTip: '请输入如“M1314”',
            })
        } else {
            await that.setData({
                dormNumTip: '',
            })
        }
    },

    /**
     * 手机号码判断
     */
    async phoneBlur(e) {
        const phone = e.detail.value
        // 判断是否为正确手机号
        if (!(/^1[3456789]\d{9}$/.test(phone)) || phone.length != 11) {
            await that.setData({
                phoneTip: '请输入正确的手机号码',
            })
        } else {
            await that.setData({
                phoneTip: '',
            })
        }
    },

    /**
     * 点击保存事件
     */
    async saveInfo() {
        let stuNum = that.data.stuNum;
        let realName = that.data.realName;
        let dormNum = that.data.dormNum;
        let phone = that.data.phone;
        // 判断内容是否填写完毕
        if (stuNum != undefined && stuNum != '' && realName != undefined && realName != '' && dormNum != undefined && dormNum != '' && phone != undefined && phone != '') {
            // 判断输入是否有错误
            if (that.data.stuNumTip == '' && that.data.realNameTip == '' && that.data.dormNumTip == '' && that.data.phoneTip == '') {
                wx.showLoading({
                    title: '保存中',
                    mask: true,
                })
                console.log('Info complete, saving');
                let user = wx.getStorageSync('user');
                user.stuNum = stuNum;
                user.realName = realName;
                user.gender = that.data.gender;
                user.dormNum = dormNum;
                user.phone = phone;
                user.is_repair = user.is_repair == undefined ? 0 : user.is_repair;
                // 数据库add/update
                let save = await mine.saveInfo(user).then(res => { return res; });
                // 是否保存成功
                if (save.result.errMsg == "collection.add:ok") {
                    console.log('add complete!');
                    wx.showToast({
                        title: '保存成功',
                        icon: 'none',
                        duration: 2000,
                    })
                    user._openid = save._openid;
                } else if (save.result.errMsg == "collection.update:ok") {
                    console.log('update complete!');
                    wx.showToast({
                        title: '保存成功',
                        icon: 'none',
                        duration: 2000,
                    })
                } else {
                    console.log('Info not save')
                    wx.showToast({
                        title: '保存失败',
                        icon: 'none',
                        duration: 2000,
                    })
                }

                // Info加入Storage缓存
                wx.setStorageSync('user', user)
                wx.hideLoading({
                    success: (res) => { },
                })
                // 跳转页面
                let to = that.data.to
                // 判断是要去哪里
                if (to == '/miniprogram/pages/mine/home/home') {
                    wx.navigateBack({
                        delta: 1,
                    })
                } else if (to == '/miniprogram/pages/CA/useRepair/userRepair') {
                    wx.redirectTo({
                        url: '../../CA/useRepair/userRepair',
                    })
                } else if (to == '/miniprogram/pages/CA/History/History') {
                    wx.redirectTo({
                        url: '../../CA/History/History',
                    })
                }
            } else {
                console.log('Info not right')
                wx.showToast({
                    title: '信息有错误',
                    icon: 'none',
                    duration: 2000,
                })
            }
        } else {
            console.log('Info not complete')
            wx.showToast({
                title: '请将信息填写完整',
                icon: 'none',
                duration: 2000,
            })
        }
    },

    // 空方法，用于双向绑定
    null() {

    },

    
    /**
     * 右上角分享
     */
    onShareAppMessage() {
        return {
            title: '首页',
            path: '/pages/CA/home/home',
            imageUrl: '../../../images/share.png'
        }
    },
})